package hfut.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hfut.example.common.R;
import hfut.example.entity.User;
import hfut.example.service.UserService;
import hfut.example.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
/*    @Autowired
    TemplateEngine templateEngine;*/
    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 发送验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        String phone = user.getPhone();
        String code = ValidateCodeUtils.generateValidateCode(4).toString();

        System.out.println();
        log.info("****code:  "+code+"****");
        System.out.println();

        //添加到session
        session.setAttribute(phone,code);

        //验证码存入redis,并设置5分钟有效
        redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
        //sendSimpleMail(code);

        return R.success("发送成功");
    }

    @PostMapping("/login")
    public R<User> login(@RequestBody Map<String,String> map, HttpSession session){
        String phone = map.get("phone");
        String code = map.get("code");

        //从session中获取验证码
        //Object codeInSession = session.getAttribute(phone);

        //从redis中获取验证码
        Object codeInSession=redisTemplate.opsForValue().get(phone);
        //比较验证码
        if(codeInSession != null && codeInSession.equals(code)){
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(queryWrapper);

            //判断用户是否注册
            if(user==null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",user.getId());

            //删除验证码
            redisTemplate.delete(phone);

            return R.success(user);
        }
        return R.error("验证码错误");
    }

    /**
     * 邮箱验证码
     * @param mail
     * @param code
     */
    public void sendSimpleMail(String mail,String code) {
        //构建邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件主题
        message.setSubject("这是一封测试邮件");
        //邮件发送者
        message.setFrom("1832913877@qq.com");
        //邮件接收者(可以有多个)
        message.setTo(mail);
        /*//邮件抄送人
        message.setCc("37xxxxx37@qq.com");*/
  /*      //邮件隐秘抄送人(可以有多个)
        message.setBcc("14xxxxx098@qq.com");*/
        //邮件发送日期
        message.setSentDate(new Date());
        //邮件正文
        message.setText(code+"   这是测试邮件的正文");
        //发送邮件
        javaMailSender.send(message);
    }

}
